<template>
  <div class="add-org">
    <!-- 详情 -->
    <cvue-dialog
      :dialogVisible="dialogVisible"
      title="创建"
      :isShowFoot="true"
      dialogWidth="65%"
      @handleOpen="getDetail"
      @closeDialog="closeDialog"
      @confirmDialog="confirmDialog"
      @handleClose="handleClose"
    >
      <div class="dialog-body" slot="dialogBody" style="padding: 0;">
        <div class="selCenter">
          <el-tabs v-model="activeName" type="card">
            <el-tab-pane label="基本信息" name="first">
                 <el-form inline label-width="60px">
                    <el-form-item label="名称" required>
                        <el-input class="formWidth" type="text" v-model.trim="schoolName" placeholder="请输入学校名称"></el-input>
                    </el-form-item>
                    <el-form-item label="编码">
                        <el-input class="formWidth" disabled="" type="text" v-model="encoding" placeholder="由系统自动生成"></el-input>
                    </el-form-item>
                    <el-form-item label="代理">
                          <treeselect style="width:216px; line-height: 16px;"
                           :maxHeight='200'
                            v-model="agentId"
                            :normalizer='normalizer'
                             placeholder='请选择代理'
                              :options="agentData" />
                      </el-form-item>
                  </el-form>
                  <area-component
                    @areaObj='areaObj'
                    :province="sheng"
                    :city="shi"
                    :county="qu"
                  ></area-component>
                  <el-form inline label-width="60px">
                    <el-form-item label="学年" required>
                          <el-select v-model="schoolYearId" placeholder="请选择">
                            <el-option
                            v-for="item in schoolYearData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            </el-option>
                        </el-select>
                      </el-form-item>
                    <el-form-item label="学段" required>
                          <el-select v-model="phaseStudyId" @change="changeSubject" placeholder="请选择">
                            <el-option
                            v-for="item in stageData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            </el-option>
                        </el-select>
                      </el-form-item>
                      <el-form-item label="科目" required>
                          <el-select v-model="subjectIds" multiple placeholder="请选择" collapse-tags @focus="cliSub">
                            <el-option
                            v-for="item in subjectData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            </el-option>
                        </el-select>
                      </el-form-item>
                      
                  </el-form>
                  <el-form inline label-width="60px">
                    <el-form-item label="账号" required>
                          <el-input class="formWidth" type="text" v-model.trim="account" placeholder="请输入教务总账号" minlength="2" maxlength="11" @blur="userDetail()"></el-input>
                          <p class="isUser" v-if="isLogin"><i class="el-icon-warning"></i>账号已存在</p>
                      </el-form-item>
                      <el-form-item label="类型" required>
                          <el-select v-model="runSchool" placeholder="请选择办学类型">
                              <el-option
                              v-for="item in dict.SchoolType"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                      </el-form-item>
                  </el-form>
                  <el-form inline label-width="60px">
                       <el-form-item label="年级" required>
                        <el-select v-model="gradeDistributions" multiple collapse-tags placeholder="请选择所配年级">
                              <el-option
                              v-for="item in gradeData"
                              :key="item.id"
                              :label="item.name"
                              :value="item.id">
                              </el-option>
                          </el-select>
                    </el-form-item>
                  </el-form>
                   <el-form inline label-width="100px">
                    <el-form-item label="学校经纬度">
                        <el-button @click="openMap" type="primary" icon="el-icon-map-location">设置经纬度</el-button>
                        <span v-if="schoolLongitude">经度： {{schoolLongitude}} , 纬度：{{schoolLatitude}} </span>
                    </el-form-item>
                 </el-form>
            </el-tab-pane>
            <!-- ------------------------------------------------------------------------------------------------------- -->
            <el-tab-pane label="扩展信息" name="second">
               <el-form inline label-width="150px">
                    <el-form-item label="学校标识码">
                        <el-input class="formWidth" type="text" v-model.trim="identifierCode" placeholder="请输入学校标识码"></el-input>
                    </el-form-item>
                    <el-form-item label="地址">
                        <el-input class="formWidth" type="text" v-model.trim="address" placeholder="请输入学校地址"></el-input>
                    </el-form-item>
                    <el-form-item label="电子邮箱">
                        <el-input class="formWidth" type="text" v-model.trim="emailAddress" placeholder="电子邮箱"></el-input>
                    </el-form-item>
                  </el-form>
                  <el-form inline label-width="150px">
                    <el-form-item label="英文名称">
                        <el-input class="formWidth" type="text" v-model.trim="englishName" placeholder="请输入学校英文名称"></el-input>
                    </el-form-item>
                    <el-form-item label="学校地址代码">
                        <el-input class="formWidth" type="text" v-model.trim="addressCode" placeholder="请输入学校地址代码"></el-input>
                    </el-form-item>
                    <el-form-item label="邮政编码">
                        <el-input class="formWidth" type="text" v-model.trim="mailCode" maxlength="10" placeholder="请输入学校邮政编码"></el-input>
                    </el-form-item>
                  </el-form>
                  <el-form inline label-width="150px">
                    <el-form-item label="驻地城乡类型代码">
                        <el-input class="formWidth" type="text" v-model.trim="urbanRural" placeholder="请输入学校驻地城乡类型代码"></el-input>
                    </el-form-item>
                    <el-form-item label="属地教育行政">
                        <el-input class="formWidth" type="text" v-model.trim="educational" placeholder="属地管理教育行政部门名称"></el-input>
                    </el-form-item>
                    <el-form-item label="属地教育行政代码">
                        <el-input class="formWidth" type="text" v-model.trim="educationalCode" placeholder="学校属地管理教育行政部门代码"></el-input>
                    </el-form-item>
                  </el-form>
                  <el-form inline label-width="150px">
                    <el-form-item label="所属教育行政">
                        <el-input class="formWidth" type="text" v-model.trim="belongEducational" placeholder="所属主管教育行政部门名称"></el-input>
                    </el-form-item>
                    <el-form-item label="所属教育行政代码">
                        <el-input class="formWidth" type="text" v-model.trim="belongEducationalCode" placeholder="学校所属主管教育行政部门代码"></el-input>
                    </el-form-item>
                    <el-form-item label="网站主页地址">
                        <el-input class="formWidth" type="text" v-model.trim="websiteUrl" placeholder="网站主页地址"></el-input>
                    </el-form-item>
                  </el-form>
                  <el-form inline label-width="150px">
                    <el-form-item label="少数民族学校">
                        <div class="formWidth">
                             <el-switch v-model="isMinority"></el-switch>
                        </div>
                    </el-form-item>
                    <el-form-item label="联系电话">
                        <el-input class="formWidth" type="number" v-model.trim="contactNumber" placeholder="联系电话"></el-input>
                    </el-form-item>
                    <el-form-item label="传真电话">
                        <el-input class="formWidth" type="number" v-model.trim="phototelephony" placeholder="传真电话"></el-input>
                    </el-form-item>
                  </el-form>
                  <!-- <el-form inline label-width="150px">
                  </el-form> -->
            </el-tab-pane>
            <!-- ------------------------------------------------------------------------------------------------------- -->
            <el-tab-pane label="详细信息" name="second2">
                <el-form inline label-width="150px">
                    <el-form-item label="校长名字">
                        <el-input class="formWidth" type="text" v-model.trim="presidentName" placeholder="校长名字"></el-input>
                    </el-form-item>
                    <el-form-item label="校长手机号码">
                        <el-input class="formWidth" type="number" v-model.trim="presidentNumber" placeholder="校长手机号码"></el-input>
                    </el-form-item>
                    <el-form-item label="学校举办者类型">
                        <el-select v-model="organizerType" placeholder="请选择学校举办者类型">
                              <el-option
                              v-for="item in dict.SchoolRunnerType"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                    </el-form-item>
                </el-form>
                <el-form inline label-width="150px">
                    <el-form-item label="附设班类型">
                        <el-select v-model="ttachedClass" placeholder="请选择附设班类型">
                              <el-option
                              v-for="item in dict.AttachedClassType"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                    </el-form-item>
                    <el-form-item label="小学学制">
                        <el-select v-model="primarySchool" placeholder="请选择小学学制">
                              <el-option
                              v-for="item in dict.PsSchoolingLength"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                    </el-form-item>
                    <el-form-item label="初中学制">
                        <el-select v-model="juniorSchool" placeholder="请选择初中学制">
                              <el-option
                              v-for="item in dict.JhsSchoolingLength"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                    </el-form-item>
                </el-form>
                <el-form inline label-width="150px">
                    <el-form-item label="高中学制">
                        <el-select v-model="highSchool" placeholder="请选择高中学制">
                               <el-option
                              v-for="item in dict.ShsSchoolingLength"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                    </el-form-item>
                    <el-form-item label="组织机构代码证号">
                        <el-input class="formWidth" type="text" v-model.trim="organizationCode" placeholder="组织机构代码证号"></el-input>
                    </el-form-item>
                    <el-form-item label="建校年份">
                        <el-date-picker
                          v-model="foundYear"
                          type="year"
                          value-format="yyyy"
                          placeholder="选择年">
                        </el-date-picker>
                    </el-form-item>
                </el-form>
                <el-form inline label-width="150px">
                    <el-form-item label="校庆日">
                        <el-date-picker
                          v-model="schoolDay"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择年">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="土地产权类型">
                        <el-select v-model="dirtType"  placeholder="请选择土地产权类型">
                              <el-option
                              v-for="item in dict.LandPropertyRightType"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                    </el-form-item>
                    <el-form-item label="土地证号">
                        <el-input class="formWidth" type="text" v-model.trim="landDeed" placeholder="土地证号"></el-input>
                    </el-form-item>
                </el-form>
                <el-form inline label-width="150px">
                    <el-form-item label="法人登记证号">
                        <el-input class="formWidth" type="text" v-model.trim="legalPerson " placeholder="法人登记证号"></el-input>
                    </el-form-item>
                    <el-form-item label="寄宿制形式">
                        <el-select v-model="boardType" placeholder="请选择寄宿制形式">
                              <el-option
                              v-for="item in dict.BoardingType"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                    </el-form-item>
                    
                </el-form>
                
                <el-form inline label-width="150px">
                    <el-form-item label="学校海拔（米）">
                        <el-input class="formWidth" type="number" v-model.trim="altitude" placeholder="学校海拔高度（米）"></el-input>
                    </el-form-item>
                    <el-form-item label="招生半径（公里）">
                        <el-input class="formWidth" type="number" v-model.trim="radii" placeholder="招生半径（公里）"></el-input>
                    </el-form-item>
                </el-form>
                <el-form inline label-width="150px">
                    <el-form-item label="主教学语言">
                        <el-input class="formWidth" type="text" v-model.trim="mainLanguage" placeholder="主教学语言"></el-input>
                    </el-form-item>
                    <el-form-item label="辅教学语言">
                        <el-input class="formWidth" type="text" v-model.trim="auxiliaryLanguage" placeholder="辅教学语言"></el-input>
                    </el-form-item>
                    <el-form-item label="学校历史沿革">
                        <el-input class="formWidth" type="textarea" v-model.trim="history" placeholder="学校历史沿革"></el-input>
                    </el-form-item>
                </el-form>
            </el-tab-pane>
          </el-tabs>          
        </div>
      </div>
    </cvue-dialog>

      <amap :dialogVisible='mapVisible' 
            :lng='schoolLongitude'
            :lat='schoolLatitude'
        @close='closeMap' 
        @comfirm='setLngLat'></amap>
  </div>
</template>
<script>
import cvueDialog from '@/components/cvue-dialog'
// import treeSelect from '@/components/treeSelect'
import areaComponent from '@/components/areaComponent'
import amap from '@/components/map'
export default {
  name: 'addSchool',
  components: {
    cvueDialog,
    areaComponent,
    amap
  },

  data () {
    return {
      mapVisible: false,
      normalizer (node) {
                return {
                    id: node.id,
                    label: node.name,
                    children: node.children.length > 0 ? node.children : 0
                }
            },
       sheng: '',
        shi: '',
        qu: '',
        activeName: 'first',
        schoolName: '', // 学校名字
        encoding: '', // 内部编码
        schoolYearId: '', // 学年
        phaseStudyId: '', // 学段
        subjectIds: [], // 科目
        account: '', // 账号
        agentId: null, // 代理
        // ================
        identifierCode: '', // 学校标识码
        address: '', // 学校地址
        runSchool: '', // 办学类型
        englishName: '', // 英文名称
        addressCode: '', // 学校地址代码
        mailCode: '', // 邮政编码
        urbanRural: '', // 驻地城乡类型代码
        educational: '', // 属地管理教育行政部门
        educationalCode: '', // 学校属地管理教育行政部门代码
        belongEducational: '', // 所属主管教育行政部门名称
        belongEducationalCode: '', // 学校所属主管教育行政部门代码
        gradeDistributions: [], // 所配年级
        isMinority: false, // 少数名族学校
        contactNumber: '', // 学校联系电话
        phototelephony: '', // 学校传真电话
        websiteUrl: '', // 学校网站主页地址
        emailAddress: '', // 电子邮箱
        // ==================
        presidentName: '', // 校长名字
        presidentNumber: '', // 校长手机号码
        organizerType: '', // 学校举办者类型
        ttachedClass: '', // 附设班类型
        primarySchool: '', // 小学学制
        juniorSchool: '', // 初中学制
        highSchool: '', // 高中学制
        organizationCode: '', // 组织机构代码证号
        foundYear: '', // 建校年份
        schoolDay: '', // 校庆日
        dirtType: '', // 土地产权类型
        landDeed: '', // 土地证号
        legalPerson: '', // 法人登记证号
        boardType: '', // 寄宿制形式
        schoolLongitude: '', // 学校经度
        schoolLatitude: '', // 学校纬度
        altitude: '', // 学校海拔
        radii: '', // 招生半径
        mainLanguage: '', // 主教学语言
        auxiliaryLanguage: '', // 辅教学语言
        history: '', // 学校历史沿革
        areaId: '', // 区域id
        areaData: '', // 省市区数据
        // ---------------------------------
        isClearable: true, // 可清空（可选）
      isAccordion: true, // 可收起（可选）
      valueId: '', // 初始ID（可选）
      props: {
        // 配置项（必选）
        value: 'id',
        label: 'name',
        children: 'children'
      },
      isLogin: false,
      subjectData: []
    }
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  mounted () {

  },
  computed: {
    gradeData () {
          return this.$store.state.schoolManage.gradeData
      },
      // subjectData () {
      //     return this.$store.state.schoolManage.subjectData
      // },
      dict () {
          return this.$store.state.schoolManage.dict
      },
      stageData () {
          return this.$store.state.schoolManage.stageData
      },
      schoolYearData () {
          return this.$store.state.schoolManage.schoolYearData
      },
      agentData () {
          return this.$store.state.schoolManage.agentData
      }
  },
  methods: {
    openMap () {
        this.mapVisible = true
    },
    closeMap () {
        this.mapVisible = false
    },
    setLngLat (obj) {
      this.schoolLongitude = obj.lng
      this.schoolLatitude = obj.lat
    },
    getValue (value) {
      this.valueId = value
      console.log(this.valueId)
    },
    // 触发事件
    handleClose () {
      this.$emit('handleClose')
    },
    closeDialog () {
      this.$emit('closeDialog')
    },
    confirmDialog () {
      if (!this.schoolName) {
        this.$message({type: 'error', message: '请输入学校名字'}) 
        return
      }
      if (!this.areaData.provinceId) {
          this.$message({
              type: 'error',
              message: '请选择省'
            }) 
          return
        }
      //   if (!this.areaData.cityId && this.areaData.isZhi) {
      //     this.$message({
      //         type: 'error',
      //         message: '请选择省市区'
      //       }) 
      //     return
      //   }
      //   if (!this.areaData.countyId && !this.areaData.isZhi) {
      //     this.$message({
      //         type: 'error',
      //         message: '请选择省市区'
      //       }) 
      //     return
      //   }
      if (!this.schoolYearId) {
        this.$message({type: 'error', message: '请选择学年'}) 
        return
      }
      if (!this.phaseStudyId) {
        this.$message({type: 'error', message: '请选择学段'}) 
        return
      }
      if (this.subjectIds.length == 0) {
        this.$message({type: 'error', message: '请选择科目'}) 
        return
      }
      if (this.account.length < 2) {
        this.$message({type: 'error', message: '请输入2-11位的教务总账号'}) 
        return
      }

      // if (!this.agentId) {
      //   this.$message({type: 'error', message: '请选择代理商'}) 
      //   return
      // } 
      if (!this.runSchool) {
        this.$message({type: 'error', message: '请选择办学类型'}) 
        return
      }
      if (this.gradeDistributions.length == 0) {
        this.$message({type: 'error', message: '请选择所配年级'}) 
        return
      }
      // if (this.areaData.countyId) {
      //     this.areaId = this.areaData.countyId
      //   } else {
      //     this.areaId = this.areaData.cityId
      //   }
      if (this.areaData.countyId) {
          this.areaId = this.areaData.countyId
        } else if (this.areaData.cityId) {
          this.areaId = this.areaData.cityId
        } else {
          this.areaId = this.areaData.provinceId
        }
      this.creatSchool()
    },
    // 获取详情
    getDetail () {
      
    },
    cliSub () {
      if (!this.phaseStudyId) {
          this.$message({
                type: 'error',
                message: '请先选择学段!'
              })
      }
    },
     changeSubject () {
      this.subjectIds = []
      this.getSubject()
    },
    // 获取学科
        getSubject () {
          var params = {
            // schoolId: this.$store.state.common.cookiesObj.schoolId,
            StageId: this.phaseStudyId,
            PageNum: 1,
            PageSize: 1000
          }
          this.$store.dispatch('schoolManage/SubjectsBySchoolId', params).then(res => {
              this.subjectData = res.items
          })
        },
    // 获取地区的数据
      areaObj (data) {
        this.areaData = data
      },
      // 判断user信息
      userDetail () {
        var params = {
              userName: this.account
        }
        this.$store.dispatch('schoolManage/UserInfo', params).then(res => {
              this.isLogin = true
          }).catch(res => {
            if (res.status == 404) {
              this.isLogin = false
            }
          })
      },
        // 创建学校  
        creatSchool () {
          if (this.isLogin) {
            return
          }
          var agentId = []
            if (this.agentId) {
                agentId.push({agentId: this.agentId})
            }
            var gradeDistributions = []
            this.gradeDistributions.forEach(item => {
              var grade = {gradeId: item, deleteFlag: true}
                gradeDistributions.push(grade)
            })
            this.gradeDistributions = gradeDistributions

            var subjectIds = []
            this.subjectIds.forEach(item => {
                 var subject = {subjectId: item, deleteFlag: true}
                subjectIds.push(subject)
            })
            this.subjectIds = subjectIds
            if (this.foundYear) {
               this.foundYear = this.foundYear + '-01-01 00:00:00'
            }
            if (this.schoolDay) {
              this.schoolDay = this.schoolDay + ' 00:00:00'
            }
           var params = {
            loginName: this.account,
            idCode: this.identifierCode,
            name: this.schoolName,
            englishName: this.englishName,
            address: this.address,
            addressCode: this.addressCode,
            postalCode: this.mailCode,
            urbanRuralTypeCode: this.urbanRural,
            locationAdminDeptName: this.educational,
            locationAdminDeptCode: this.educationalCode,
            deptInChargeName: this.belongEducational,
            deptInChargeCode: this.belongEducationalCode,
            schoolTypeCode: this.runSchool,
            schoolRunnerTypeCode: this.organizerType,
            stageId: this.phaseStudyId,
            areaId: this.areaId,
            currentSchoolYearId: this.schoolYearId,
            minoritySchoolFlag: this.isMinority,
            schoolmasterName: this.presidentName,
            schoolmasterCellPhoneNum: parseInt(this.presidentNumber),
            contactPhoneNum: parseInt(this.contactNumber),
            faxPhoneNum: parseInt(this.phototelephony),
            websiteUrl: this.websiteUrl,
            email: this.emailAddress,
            attachedClassTypeCode: this.ttachedClass,
            psSchoolingLengthCode: this.primarySchool,
            jhsSchoolingLengthCode: this.juniorSchool,
            shsSchoolingLengthCode: this.highSchool,
            orgCodeCertifNum: this.organizationCode,
            schoolFoundedYear: this.foundYear,
            legalPersonRegCertifNum: this.legalPerson,
            anniversaryDate: this.schoolDay,
            landPropertyRightTypeCode: this.dirtType,
            landCertifNum: this.landDeed,
            boardingTypeCode: this.boardType,
            aboveSeaLevel: parseInt(this.altitude),
            longitude: parseFloat(this.schoolLongitude),
            latitude: parseFloat(this.schoolLatitude),
            enrollmentRadius: parseInt(this.radii),
            mainTeachingLanguage: this.mainLanguage,
            auxTeachingLanguage: this.auxiliaryLanguage,
            schoolHistory: this.history,
            schoolgrademapping: this.gradeDistributions, // 所配年级
            schoolsubjectmapping: this.subjectIds, // 科目
            agentschoolmapping: agentId // 代理商
          }
          this.$store.dispatch('schoolManage/CreatSchool', params).then(res => {
              this.$message({
                type: 'success',
                message: '创建成功!'
              })
              this.$emit('confirmDialog')
          })
        }
  }
}
</script>
<style scoped>
  .selOption{
    display: inline-block;
      width: 235px;
      margin-bottom: 10px;
  }
  .selOption span{color: red;}
  .selCenter{
    display: inline-block;
  }
  .formWidth{
          display: inline-block;
          width: 215px;
      }
  .isUser{
    color: red;
    font-size: 14px;
  }
</style>
